﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aliyeye.Car.Net.UI.SystemHelp;
using Aliyeye.Car.Net.UI.PopupDialog;
using Aliyeye.Car.Net.UI.Common;

namespace Aliyeye.Car.Net.UI.CarModel
{
    public partial class AddCar_Insure : BaseForm
    {
        CarInsureSvc.SoapHeaderClass sopaHeader = new Aliyeye.Car.Net.UI.CarInsureSvc.SoapHeaderClass();
        private bool isDriver = false;
        private int RepId = 0;
        private string pageInfo = null;
        public AddCar_Insure()
        {
            InitializeComponent();
        }
         public AddCar_Insure(bool isDri)
        {
            InitializeComponent();
            isDriver = isDri;
        }

         public AddCar_Insure(bool isDri, int id, string edit)
        {
            InitializeComponent();
            isDriver = isDri;
            RepId= id;
            pageInfo = edit;
        }

        private void AddCar_Insure_Load(object sender, EventArgs e)
        {
            if (RepId != 0)
            {
                BindInfo(); btnCancel.Text = "返回";
            }
            else
            {
                txtuser.Text = Login.LogUserName.ToString();
            }
        }
        private void BindInfo()
        {

            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            CarInsureSvc.Car_Insure model = new Aliyeye.Car.Net.UI.CarInsureSvc.Car_Insure();
            ServiceManipulator.SafeAsyncExecution<CarInsureSvc.Car_InsureSoapClient>(
                proxy => proxy.GetModel(sopaHeader, RepId),
                result =>
                {
                    if (result.Error == null)
                    {
                        model = (CarInsureSvc.Car_Insure)result.Result;
                        txtcarcode.Text = model.CarCode.ToString();
                        txtcomaddr.Text = model.ComAddr.ToString();
                        txtComname.Text = model.ComName.ToString();
                        txtcomtel.Text = model.ComTel.ToString();
                        txtcomuser.Text = model.ComUser.ToString();
                        txtend.Value = model.EndTime;
                        txtmark.Text = model.Mark.ToString();
                        txtProject.Text = model.Project.ToString();
                        txtstar.Value = model.StarTime;
                        txtprice.Text = model.Price.ToString();
                        txtuser.Text = model.InputUserName;
                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
            );

            if (pageInfo == "look")
            {
                txtcarcode.Enabled = false;
                txtcomaddr.Enabled = false; txtComname.Enabled = false;
                txtcomtel.Enabled = false; txtcomuser.Enabled = false;
                txtend.Enabled = false; txtmark.Enabled = false;
                txtprice.Enabled = false; txtProject.Enabled = false;
                txtstar.Enabled = false; txtuser.Enabled = false;
            }

        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtcarcode.Text))
            {
                DialogWarning dia = new DialogWarning("车辆编号不能为空！");
                dia.ShowDialog();
                return;
            }
            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            if (string.IsNullOrEmpty(txtprice.Text))
            {
                txtprice.Text = "0";
            }

            CarInsureSvc.Car_Insure model = new Aliyeye.Car.Net.UI.CarInsureSvc.Car_Insure();

            model.CarCode = txtcarcode.Text;
            model.ComAddr = txtcomaddr.Text;
            model.ComName = txtComname.Text.ToString();
            model.ComTel = txtcomtel.Text.ToString();
            model.ComUser = txtcomuser.Text.ToString();
            model.EndTime = txtend.Value;
            model.Mark = txtmark.Text;
            model.Project = txtProject.Text;

            model.Price = decimal.Parse(txtprice.Text);
            model.StarTime = txtstar.Value;
            model.UserId = Login.LogUserId;
            model.InputTime = DateTime.Now;
            if (RepId > 0)
            {
                model.Id = RepId;
                ServiceManipulator.SafeAsyncExecution<CarInsureSvc.Car_InsureSoapClient>(
               proxy => proxy.Update(sopaHeader, model),
               result =>
               {
                   if (result.Error == null)
                   {
                       PageCommon.Notify("保险信息修改成功！");
                       overlay.FadeOut();Cursor.Current = Cursors.Default;
                   }
                   else
                   {
                       PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                   }
               },
               this
               );
            }
            else
            {
                ServiceManipulator.SafeAsyncExecution<CarInsureSvc.Car_InsureSoapClient>(
                proxy => proxy.Add(sopaHeader, model),
                result =>
                {
                    if (result.Error == null)
                    {
                        PageCommon.Notify("保险信息添加成功！");
                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
            );

            }
            clear();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void clear()
        {
            txtcarcode.Text = "";
            txtcomaddr.Text = "";
            txtComname.Text = "";
            txtcomtel.Text = "";
            txtcomuser.Text = "";
            txtmark.Text = "";
            txtprice.Text = "";
            txtProject.Text = "";
        }
        private bool IsNumberMoney(System.Windows.Forms.TextBox t, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == '.' || e.KeyChar == 8 || e.KeyChar == 13))
            {
                //lb.Text=string.Empty;
                return true;
            }
            else
            {
                DialogNotify diaSecc = new DialogNotify("只能输入数字！");
                diaSecc.ShowDialog();
                e.Handled = true;
                return false;
            }
        }

        private void txtprice_KeyPress(object sender, KeyPressEventArgs e)
        {
            IsNumberMoney(txtprice, e);
        }

        #region 车牌号 输入框数据绑定

        string driverName = string.Empty;
        private void txtCarCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string strSearch = this.txtcarcode.Text;
                if (string.IsNullOrEmpty(strSearch))
                {
                    DialogWarning dia = new DialogWarning("车牌号不能为空！");
                    dia.ShowDialog();
                    return;
                }
                else
                {
                    DisplayCustomeLIst(strSearch, "车牌号");
                }
            }
        }

        private void DisplayCustomeLIst(string strSearch, string type)
        {
            if (string.IsNullOrEmpty(strSearch))
            {
                return;
            }
            string strSelectWhere = "";
            if (type == "车牌号")
            {
                strSelectWhere = " Code like '%" + strSearch + "%'";
            }
            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            DataSet ds = null;
            this.dgvDriverInfo.DataSource = null;
            dgvDriverInfo.AutoGenerateColumns = false;
            CarSvc.SoapHeaderClass sopaHeader = new Aliyeye.Car.Net.UI.CarSvc.SoapHeaderClass();
            ServiceManipulator.SafeAsyncExecution<CarSvc.CarServiceSoapClient>(
                proxy => proxy.GetList(sopaHeader, strSelectWhere),
                result =>
                {
                    if (result.Error == null)
                    {
                        ds = (DataSet)result.Result;
                        if (ds != null && ds.Tables[0].Rows.Count > 0)
                        {
                            dgvDriverInfo.DataSource = ds.Tables[0];
                            this.dgvDriverInfo.Visible = true;
                            this.dgvDriverInfo.Focus();
                        }
                        else
                        {
                            PageCommon.Alert("查找的车辆不存在，请重新查询！");
                            txtcarcode.Text = driverName;
                        }
                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
            );

        }

        private void dgvDriveInfo_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (this.dgvDriverInfo.Rows.Count != 0)
            {
                for (int i = 0; i < this.dgvDriverInfo.Rows.Count; )
                {
                    this.dgvDriverInfo.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.PapayaWhip;
                    i += 2;
                }
            }
        }

        private void dgvInfo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                this.txtcarcode.Text = this.dgvDriverInfo.CurrentRow.Cells["车牌号1"].Value.ToString();
                //  driverID = Convert.ToInt32(this.dgvInfo.CurrentRow.Cells["编号"].Value.ToString());
                driverName = this.txtcarcode.Text;
                this.dgvDriverInfo.Visible = false;
                this.txtComname.Focus();
            }
        }

        private void dgvInfo_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {

            e.CellStyle.WrapMode = DataGridViewTriState.True;
            if (this.dgvDriverInfo.Columns[e.ColumnIndex].Name == "性别")
            {
                if (e.Value != null)
                {
                    if (Convert.ToBoolean(e.Value))
                    {
                        e.Value = "带驾";
                    }
                    else
                    {
                        e.Value = "自驾";
                    }
                }
            }

        }

        #endregion 驾驶员输入框数据绑定

    }
}
